package com.moge.anjian.manager;

import android.util.Log;

import com.moge.anjian.MogeAccessibilityManager;
import com.moge.anjian.utils.MissionCallback;
import com.moge.anjian.view.TargetView;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AutoClickManager {

    private static AutoClickManager instance = new AutoClickManager();

    ExecutorService execute = Executors.newSingleThreadExecutor();
    MissionCallback callback;
    volatile boolean missionRunning;

    private AutoClickManager() {
    }

    public static AutoClickManager getInstance() {
        return instance;
    }

    public void setOnMissionCallback(MissionCallback callback) {
        this.callback = callback;
    }

    public void startAutoClick(float x, float y, int count, long delay, long interval, long duration) {

        if (TargetManager.getInstance().getViewList() == null) {
            return;
        }
        for (TargetView targetView : TargetManager.getInstance().getViewList()) {
            targetView.setTapPenetrate(true);
        }
        missionRunning = true;
        execute.submit(new Runnable() {
            @Override
            public void run() {
                Log.e("dimogee", "任务Run");
                while (missionRunning) {
                    Log.e("dimogee", "任务Running......x" + x + ", y=" + y);
                    try {
                        if (delay != 100) {
                            Thread.sleep(delay);
                        }
                        MogeAccessibilityManager.performClick(x, y);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Log.e("dimogee", "InterruptedException 没关系");
                    }
                }
            }
        });
    }

    public void stopAutoClick() {
        missionRunning = false;
        for (TargetView targetView : TargetManager.getInstance().getViewList()) {
            targetView.setTapPenetrate(false);
        }
    }

    public boolean isMissionRunning() {
        return missionRunning;
    }

}
